package dataStructure;


import bean.People;

/**
 * @author xuxin
 */
public class DoubleSingleLinkList {

    public static void main(String[] args) {
        DoubleSingleLinkList list = new DoubleSingleLinkList();
//        list.add(new People(13, "张三"));
//        list.add(new People(1, "张三"));
//        list.add(new People(2, "张三"));
//        list.add(new People(3, "张三"));
//        list.add(new People(8, "张三"));
//        list.add(new People(11, "张三"));
//        list.add(new People(5, "张三"));
//        list.add(new People(6, "张三"));
//        list.add(new People(4, "张三"));
//        list.add(new People(7, "张三"));
//        list.add(new People(16, "张三"));
//        list.add(new People(9, "张三"));
//        list.add(new People(10, "张三"));
//        list.add(new People(15, "张三"));
//        list.add(new People(14, "张三"));
//        list.show();


        list.addByArray(new People(13, "张三"));
        list.addByArray(new People(1, "张三"));
        list.addByArray(new People(2, "张三"));
        list.addByArray(new People(3, "张三"));
        list.addByArray(new People(8, "张三"));
        list.addByArray(new People(11, "张三"));
        list.addByArray(new People(5, "张三"));
        list.addByArray(new People(6, "张三"));
        list.addByArray(new People(4, "张三"));
        list.addByArray(new People(7, "张三"));
        list.addByArray(new People(16, "张三"));
        list.addByArray(new People(9, "张三"));
        list.addByArray(new People(10, "张三"));
        list.addByArray(new People(15, "张三"));
        list.addByArray(new People(14, "张三"));


        list.delete(7);
        list.show();
    }

    public People head = new People(-1, "");

    /**
     * 新增节点,没有顺序，按照
     */
    public void add(People people) {
        People temp = head;
        boolean flag = true;
        while (flag) {
            if (temp.getNext() != null) {
                temp = temp.getNext();
            } else {
                temp.setNext(people);
                people.setPre(temp);
                flag = false;
            }
        }
    }

    /**
     * 按照顺序添加,按照no从小到大
     */
    public void addByArray(People people) {
        People temp = head;
        boolean flag = true;
        while (flag) {
            //如果当前这个节点的pre为空，说明当前这个节点是头结点,直接在头结点后面添加就可以
            if (temp.getPre() == null && temp.getNext() == null) {
                temp.setNext(people);
                people.setPre(temp);
                flag = false;
            } else if (temp.getNo() > people.getNo()) {
                temp.getPre().setNext(people);
                people.setPre(temp.getPre());
                people.setNext(temp);
                temp.setPre(people);
                flag = false;
            }
            //说明temp已经是最后一个节点了，people直接加在temp后面
            else if (temp.getNext() == null) {
                people.setPre(temp);
                temp.setNext(people);
                flag = false;
            } else {
                temp = temp.getNext();
            }
        }
    }

    /**
     * 根据no删除节点
     */
    public void delete(int no) {
        People temp = head.getNext();
        boolean flag = true;
        while (flag) {
            if (temp == null) {
                throw new RuntimeException("没有需要删除的数据");
            } else if (temp.getNo() == no) {
                temp.getPre().setNext(temp.getNext());
                temp.getNext().setPre(temp.getPre());
                flag = false;
            } else {
                temp = temp.getNext();
            }
        }
    }

    /**
     * 遍历打印出链表数据
     */
    public void show() {
        People temp = head.getNext();
        while (temp != null) {
            System.out.println(temp);
            temp = temp.getNext();
        }
    }
}

